import { request } from "../../request/index.js";
import regeneratorRuntime from '../../lib/runtime/runtime';
Page({
  username: "",
  password: "",
  handleUsername(e){
    const {value} = e.detail;
    this.username = value;
  },
  handlePassword(e){
    const {value} = e.detail;
    this.password = value;
  },
  handleConfirm(){
    const data = {
      user_name: this.username,
      user_code: this.password
    }
    wx.showLoading({
      title: "加载中",
      mask: true
    });
    wx.request({
      url: 'http://47.94.130.28:8000/user/login/',
      data: data,
      method: "POST",
      success(res) {
        wx.hideLoading()
        const user_info = res.data;
        console.log(user_info)
        if (user_info.valid) {
          wx.setStorageSync("user_info", user_info);
          if (getCurrentPages().length == 1) {
            wx.reLaunch({
              url: '/pages/user',
            })
          } else {
            wx.navigateBack();
          }
        } else {
          wx.showModal({
            title: "登陆失败",
            content: "登陆失败！请检查用户名和密码，或稍后再试",
            showCancel: false
          })
        }
      }
    })
    // const user_info = {
    //   valid: true,
    //   uid: "12345678",
    //   user_name: "bujiangwude",
    //   nick_name: "TaichiMa",
    //   image_src: "/static/g1.jpg",
    //   user_email: "mabaoguo17@mails.tsinghua.edu.cn",
    //   user_phone: "18012345678",
    //   user_vxid: "wx_awkl8h1b35k",
    //   user_status: true
    // }
    // if (user_info.valid) {
    //   wx.setStorageSync("user_info", user_info);
    //   if (getCurrentPages().length == 1) {
    //     wx.reLaunch({
    //       url: '/pages/user',
    //     })
    //   } else {
    //     wx.navigateBack();
    //   }
    // } else {
    //   wx.showModal({
    //     title: "登陆失败",
    //     content: "登陆失败！请检查用户名和密码，或稍后再试",
    //     showCancel: false
    //   })
    // }
  },

  handleRegister() {
    wx.navigateTo({
      url: '/pages/register/register',
    })
  }
})